<template>
  <div class="meat">
    <!-- 一级菜单下面所拥有的二级菜单 -->
    <el-aside class="aside" :width="isCollapse ? '50px' : '200px'">
      <i
        class="el-icon-s-fold toggle-button"
        @click="toggleCollapse"
        v-if="isCollapse == false"
      ></i>
      <i
        class="el-icon-s-unfold toggle-button"
        @click="toggleCollapse"
        v-else
      ></i>
      <el-menu
        ref="menu"
        :default-active="activePath"
        router
        unique-opened
        :collapse-transition="false"
        :collapse="isCollapse"
        class="sidebar el-menu-vertical-demo"
        @select="openMenu"
        text-color="#A9B0B7"
        active-text-color="#0064F1"
      >
        <menu-tree :menuData="treeData"></menu-tree>
      </el-menu>
    </el-aside>
    <!-- 以及二级菜单所对应的页面 -->
    <el-main class="main_container">
      <router-view></router-view>
    </el-main>
  </div>
</template>

<script>
import menuTree from "@/components/commonPublic/menu.vue";
import {
  getBaseModuleList
} from "@/api/request.js";
export default {
  components: {
    // SideMenu
    menuTree,
  },
  data() {
    return {

      // activePath: "entryLedger",
      activePath: "",

      isCollapse: false,
      // 场站生产
      treeData: [
        // 标段WBS工程划分
        {
          title: "标段WBS工程划分",
          img: "iconfont icon-xitongguanli1",
          name: "WbsProjectManage",
          left: 0,
          children: [
            {
              title: "标段工程划分",
              path: "ProjectPage",
              path2: "ProjectPageText",
              name: "ProjectPage",
              name2: "ProjectPageText",
              img: "iconfont icon-shengchanguanli1",
              children: [],
              left: 37,
            },
          ],
        },
        {
          title: "工序报验质量管控",
          img: "iconfont icon-xitongguanli1",
          name: "ProcessInspectionQualityManage",
          left: 0,
          children: [
            {
              title: "一次性合格率",
              path: "oneTimePassRate",
              path2: "oneTimePassRateText",
              name: "oneTimePassRate",
              name2: "oneTimePassRateText",
              img: "iconfont icon-shengchanguanli1",
              children: [],
              left: 37,
            },
          ],
        },
        // 质量责任网格管理
        {
          title: "质量责任网格管理",
          img: "iconfont icon-xitongguanli1",
          name: "QualityResponsibility",
          left: 0,
          children: [
            {
              path: "stationQualityList",
              path2: "stationQualityListText",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "质量网格管理",
              name: "stationQualityList",
              name2:"stationQualityListText",
              left: 37,
            },
            {
              path: "startLook",
              path2: "startLookText",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "网格开工管理",
              name: "startLook",
              name2:"startLookText",
              left: 37,
            },
            {
              path: "responsibility",
              path2: "responsibilityText",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "管理网格管理",
              name: "responsibility",
              name2:"responsibilityText",
              left: 37,
            },

          ],
        },
        // 工作指令动态管理
        {
          title: "工作指令动态管理",
          img: "iconfont icon-xitongguanli1",
          name: "workOrderManage",
          left: 0,
          children: [
            {
              path: "distribute",
              path2: "distributeText",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "工作指令下发",
              name: "distribute",
              name2:"distributeText",
              left: 37,
            },
            {
              path: "execute",
              path2: "executeText",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "工作指令复查",
              name: "execute",
              name2:"executeText",
              left: 37,
            },
            {
              path: "communicate",
              path2: "communicateText",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "工作在线沟通",
              name: "communicate",
              name2:"communicateText",
              left: 37,
            },

          ],
        },
        // 现场质量巡查
        {
          title: "现场质量巡查",
          img: "iconfont icon-xitongguanli1",
          name: "patrolManage",
          left: 0,
          children: [
            {
              path: "patrolInformation",
              path2: "patrolInformationText",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "质量巡查信息",
              name: "patrolInformation",
              name2:"patrolInformationText",
              left: 37,
            },
            {
              path: "patrolRectification",
              path2: "patrolRectificationText",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "巡查整改反馈",
              name: "patrolRectification",
              name2:"patrolRectificationText",
              left: 37,
            },
           

          ],
        },
         // 软基施工质量管控
         {
          title: "软基施工质量管控",
          img: "iconfont icon-xitongguanli1",
          name: "softFoundationManage",
          left: 0,
          children: [
            {
              path: "pileDataManage",
              path2: "",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "成桩数据管理",
              name: "pileDataManage",
              name2:"",
              left: 37,
            },
            {
              path: "overLimitMonitor",
              path2: "",
              img: "iconfont icon-jianzhucailiao",
              children: [],
              title: "超标数据处理",
              name: "overLimitMonitor",
              name2:"",
              left: 37,
            },
           

          ],
        },
      ],
    };
  },
  watch: {
    activePath() {
      this.activePath = sessionStorage.getItem("yjSmart-keyPath");
    },
  },
  mounted() {
    //  this.$refs.kzMenu.activedIndex = path;
    // defaultMenu
    this.activePath =
      sessionStorage.getItem("yjSmart-keyPath") || "entryLedger";
      this.roleModule()
  },
  methods: {
    roleModule() {
      var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
      var fristModule = [];
      if (ModuleList.length != 0) {
        this.treeData.map((i) => {
          //筛选大模块
          ModuleList.map((item) => {
            if (item.fEncode == i.name) {
              i.title = item.fFullname;
              fristModule.push(i);
            }
          });
        });

        fristModule.map((item) => {
          //只有两级
          item["childrens"] = [];
          item.children.map((i) => {
            ModuleList.map((j) => {
              if (i.name == j.fEncode) {
                // console.log(i, j);
                i.title = j.fFullname;
                item["childrens"].push(i);
              }
            });
          });
          item.children = item.childrens;
        });
        // 有三级
        fristModule.map((item) => {
          item.children.map((i) => {
            //二级
            i["childrens"] = [];
            i.children.map((h) => {
              //循环3级
              ModuleList.map((j) => {
                if (h.name == j.fEncode) {
                  h.title = j.fFullname;
                  i["childrens"].push(h);
                }
              });
            });

            i.children = i.childrens;
          });
        });

        fristModule.map((item) => {
          item.children.map((i) => {
            if (i.rank == 2) {
              i.path2 = "";
       
              ModuleList.map((j) => {
                if (i.name2 == j.fEncode) {
                  i.path2 = i.path + "Text";
                }
              });
            }
          });
        });

        this.treeData = fristModule;
      } else if (
        JSON.parse(sessionStorage.getItem("yjSmart-userInfo")).enCode == "System"
      ) {
        this.getTree();
      } else {
        this.treeData = [];
        return this.$message.error("请配置对应权限");
      }
    },
    leftNameDataData(val, data) {
      // val:树接口  data:本地
      let list = [];
      if (!Array.isArray(data)) {
        return list;
      }
      list = data.map((item) => {
        if (item.children.length > 0) {
          this.leftNameDataData(val, item.children);
        }
        val.map((i) => {
          // 接口等于本地
          if (i.fEncode == item.name) {
            item.title = i.fFullname;
          }
        });

        return item;
      });

      return list;
    },
    getTree() {
      getBaseModuleList({
        fModuleid: "",
      })
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.baseModuleList;
            this.treeData = this.leftNameDataData(
              companyTree,
              this.treeData
            );
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },
    // 左侧菜单是否显示
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    openMenu(keyPath) {

      if (keyPath != "null") {
        sessionStorage.setItem("yjSmart-keyPath", keyPath);
      }
    },
    handleNodeClick(data) {
      console.log(data, 3333);
    },

  },
};
</script>
